package cn.wolfcode.luowowo.community.service.impl;

import cn.wolfcode.luowowo.community.domain.CommunityAnswer;
import cn.wolfcode.luowowo.community.domain.CommunityQuestion;
import cn.wolfcode.luowowo.community.service.ICommunityAnswerService;
import cn.wolfcode.luowowo.community.service.ICommunityQuestionService;
import cn.wolfcode.luowowo.community.service.ICommunityQuestionVOService;
import cn.wolfcode.luowowo.community.vo.CommunityQuestionVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommunityQuestionVOServiceImpl implements ICommunityQuestionVOService{

    @Reference
    private ICommunityQuestionService communityQuestionService;

    @Reference
    private ICommunityAnswerService communityAnswerService;

    public List<CommunityQuestionVO> query() {
        // 先查出所有的CommunityQuestion
        List<CommunityQuestion> questions = communityQuestionService.query();
        List<CommunityQuestionVO> voList = new ArrayList<>();

        // 遍历问题,将问题对应的金牌回答放入voList中
        for (CommunityQuestion question : questions) {
            CommunityAnswer answer = communityAnswerService.queryGoldReplyByQuestionId(question.getId());
            CommunityQuestionVO vo = new CommunityQuestionVO();
            vo.setQuestionId(question.getId());
            vo.setTitle(question.getTitle());
            vo.setTags(question.getTagList());
            vo.setReplynum(question.getReplynum());
            vo.setViewnum(question.getViewnum());
            vo.setReleaseTime(question.getReleaseTime());
            vo.setDestName(question.getDest().getName());
            vo.setDestId(question.getDest().getId());
            if (answer != null) {
                vo.setIdOfGoldReplier(answer.getUserId());
                vo.setNickNameOfGoldReplier(answer.getUsername());
                vo.setHeadImgUrlOfGoldReplier(answer.getHeadImgUrl());
                vo.setLevelOfGoldReplier(answer.getLevel());
                String content = answer.getContent();
                if (content.length() >= 362) {
                    vo.setSummaryOfGoldReply(content.substring(0, 362) + "...");
                } else {
                    vo.setSummaryOfGoldReply(content);
                }
                vo.setThumbnumOfGoldReply(answer.getThumbnum());
            }
            voList.add(vo);
        }
        // 遍历,查出所有问题对应的金牌回答CommunityAnswer
        // 拼成vo

        return voList;
    }
}
